package com.ruoyi.web.controller.specific;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.specific.domain.bo.ImprovingUserPointsBo;
import com.ruoyi.specific.domain.vo.ImprovingUserPointsImportVo;
import com.ruoyi.specific.domain.vo.ImprovingUserPointsVo;
import com.ruoyi.specific.listener.ImprovingUserPointsImportListener;
import com.ruoyi.specific.service.IImprovingUserPointsService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 用户积分
 *
 * @author ruoyi
 * @date 2024-01-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/specific/userPoints")
public class ImprovingUserPointsController extends BaseController {

    private final IImprovingUserPointsService iImprovingUserPointsService;

    /**
     * 查询用户积分列表
     */
    @SaCheckPermission("specific:userPoints:list")
    @GetMapping("/list")
    public TableDataInfo<ImprovingUserPointsVo> list(ImprovingUserPointsBo bo, PageQuery pageQuery) {
        return iImprovingUserPointsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出全部用户积分列表
     */
    @SaCheckPermission("specific:userPoints:export")
    @Log(title = "用户积分", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAll")
    public void exportAll(ImprovingUserPointsBo bo, HttpServletResponse response) {
        List<ImprovingUserPointsVo> list = iImprovingUserPointsService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户积分", ImprovingUserPointsVo.class, response);
    }

    /**
     * 导出选择用户积分列表
     */
    @SaCheckPermission("specific:userPoints:export")
    @Log(title = "用户积分", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelect/{userIds}")
    public void exportSelect(@NotEmpty(message = "主键不能为空")
                                 @PathVariable Collection<Long> userIds, HttpServletResponse response) {
        List<ImprovingUserPointsVo> list = iImprovingUserPointsService.queryByIds(userIds);
        ExcelUtil.exportExcel(list, "用户积分", ImprovingUserPointsVo.class, response);
    }

    /**
     * 获取用户积分详细信息
     *
     * @param userId 主键
     */
    @SaCheckPermission("specific:userPoints:query")
    @GetMapping("/{userId}")
    public R<ImprovingUserPointsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userId) {
        return R.ok(iImprovingUserPointsService.queryById(userId));
    }

    /**
     * 新增用户积分
     */
    @SaCheckPermission("specific:userPoints:add")
    @Log(title = "用户积分", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ImprovingUserPointsBo bo) {
        return toAjax(iImprovingUserPointsService.insertByBo(bo));
    }

    /**
     * 修改用户积分
     */
    @SaCheckPermission("specific:userPoints:edit")
    @Log(title = "用户积分", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ImprovingUserPointsBo bo) {
        return toAjax(iImprovingUserPointsService.updateByBo(bo));
    }

    /**
     * 删除用户积分
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("specific:userPoints:remove")
    @Log(title = "用户积分", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userIds) {
        return toAjax(iImprovingUserPointsService.deleteWithValidByIds(Arrays.asList(userIds), true));
    }


    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "项目得分",  ImprovingUserPointsImportVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @SaCheckPermission("specific:userPoints:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<ImprovingUserPointsImportVo> result = ExcelUtil.importExcel(file.getInputStream(), ImprovingUserPointsImportVo.class, new ImprovingUserPointsImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }
}
